from rest_framework.generics import ListAPIView,CreateAPIView,ListCreateAPIView
from meiduo_admin.serializers.user import UserSerializer
from meiduo_admin.utils import PageNum

from users.models import User


class UserView(ListCreateAPIView):
    # queryset = User.objects.all()
    serializer_class = UserSerializer
    pagination_class = PageNum

    # 重写queryset方法，根据前端请求返回不同的数据内容
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword is '':
            return User.objects.all()
        return User.objects.filter(username=keyword).all()
